home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / rs232.exe / RS232.C < prev    next >
Text File  |  1993-05-03  |  45KB  |  1,284 lines

  1. /* rs232.h v1.5 */
  2.  
  3. /* port characteristic definitions for use with rs_initport */
  4.    /* port id's */
  5. #define RS_PORT1 '1'       /* port 1 */
  6. #define RS_PORT2 '2'       /*   .    */
  7. #define RS_PORT3 '3'       /*   .    */
  8. #define RS_PORT4 '4'       /* port 4 */
  9.    /* baud rates */
  10. #define RS_B110 110L       /* 110 baud */
  11. #define RS_B300 300L       /*    .     */
  12. #define RS_B600 600L       /*    .     */
  13. #define RS_B1200 1200L     /*    .     */
  14. #define RS_B2400 2400L     /*    .     */
  15. #define RS_B4800 4800L     /*    .     */
  16. #define RS_B9600 9600L     /*    .     */
  17. #define RS_B19K 19200L     /*    .     */
  18. #define RS_B38K 38400L     /*    .     */
  19. #define RS_B57K 57600L     /*    .     */
  20. #define RS_B115K 115200L   /* 115200 baud */
  21.    /* parity */
  22. #define RS_NOPAR 'N'       /* no parity */
  23. #define RS_EVPAR 'E'       /* even parity */
  24. #define RS_ODPAR 'O'       /* odd parity */
  25. #define RS_SPPAR 'S'       /* space parity */
  26. #define RS_MKPAR 'M'       /* mark parity */
  27.    /* data bits */
  28. #define RS_DBIT8 '8'       /* 8 data bits */
  29. #define RS_DBIT7 '7'       /* 7 data bits */
  30.    /* stop bits */
  31. #define RS_SBIT1 '1'       /* 1 stop bit */
  32. #define RS_SBIT2 '2'       /* 2 stop bits */
  33. /* values returned by rs_initport */
  34. #define RS_UART4 4         /* init. OK, UART is 16550AF,C,CF */
  35. #define RS_UART3 3         /* init. OK, UART is 16550 */
  36. #define RS_UART2 2         /* init. OK, UART is 8250A or 16450 */
  37. #define RS_UART1 1         /* init. OK, UART is 8250 or 8250B */
  38. #define RS_NOUART 0        /* init. failed - no UART detected */
  39. #define RS_BADIBUF -1      /* init. failed - in. buffer or size bad */
  40. #define RS_BADOBUF -2      /* init. failed - out. buffer or size bad */
  41. #define RS_BADPORT -3      /* init. failed - invalid port argument */
  42. #define RS_BADPAR -4       /* init. failed - invalid parity argument */
  43. #define RS_BADDBIT -5      /* init. failed - invalid data bits argument */
  44. #define RS_BADSBIT -6      /* init. failed - invalid stop bits argument */
  45. #define RS_BADBAUD -7      /* init. failed - invalid baud argument */
  46.  
  47. /* bit definitions to test value returned by rs_error */
  48. #define RS_RBER 0x01       /* Receive data overrun (receive buffer) */
  49. #define RS_ROER 0x02       /* Receive data overrun (UART) */
  50. #define RS_PERR 0x04       /* Parity error */
  51. #define RS_FERR 0x08       /* Framing error */
  52. #define RS_BKDT 0x10       /* Break detected */
  53. #define RS_FFER 0x80       /* Error in receive FIFO */
  54.  
  55. /* definitions for use with rs_modctrl */
  56. #define RS_GETMSR 0 /* command to read state of modem status lines */
  57.   /* bit definitions to test value returned with RS_GETMSR cmd */
  58.   #define RS_CTSCHG 0x01   /* CTS changed states */
  59.   #define RS_DSRCHG 0x02   /* DSR changed states */
  60.   #define RS_RICHG  0x04   /* RI changed states */
  61.   #define RS_DCDCHG 0x08   /* DCD changed states */
  62.   #define RS_CTSSTE 0x10   /* state of CTS */
  63.   #define RS_DSRSTE 0x20   /* state of DSR */
  64.   #define RS_RISTE  0x40   /* state of RI */
  65.   #define RS_DCDSTE 0x80   /* state of DCD */
  66. #define RS_WRTMCR 1 /* command to control hardware control lines */
  67.   /* parameter1 definitions for use with RS_WRTMCR and RS_GETMCR commands */
  68.   #define RS_MCRDTR 1      /* DTR line */
  69.   #define RS_MCRRTS 2      /* RTS line */
  70.   /* parameter2 definitions for use with RS_WRTMCR command */
  71.   #define RS_LINON 1       /* turn selected line on */
  72.   #define RS_LINOFF 0      /* turn selected line off */
  73. #define RS_GETMCR 2 /* command to return current state of modem control reg. */
  74.  
  75. /* command definitions for rs_timer */
  76. #define RS_CLRTIM 0 /* return timer count, set timer to 0 */
  77. #define RS_GETTIM 1 /* return timer count, do not clear */
  78.  
  79. /* definitions for rs_setflow */
  80. #define RS_FLWOFF 0 /* command to turn flow control off */
  81. #define RS_FLWHDW 1 /* command to turn hardware flow control on */
  82.   /* parameter1 bit definitions for use with RS_FLWHDW command */
  83.   #define RS_FLWCTS 1      /* use CTS line */
  84.   #define RS_FLWDSR 2      /* use DSR line */
  85.   #define RS_FLWRI  4      /* use RI line */
  86.   #define RS_FLWDCD 8      /* use DCD line */
  87. #define RS_FLWXON 2 /* command to turn XON/XOFF flow control on */
  88.   /* parameter1 and parameter2 definitions for use with RS_FLWXON */
  89.   #define RS_XON 0x11      /* standard XON character */
  90.   #define RS_XOFF 0x13     /* standard XOFF character */
  91. #define RS_FLWSTAT 3 /* command to return status of flow control */
  92.   /* values returned by RS_FLWSTAT command */
  93.   #define RS_FLWHLT 1      /* output halted by flow control */
  94.   #define RS_FLWNHLT 0     /* output not halted by flow control */
  95. #define RS_FLWINS 4 /* command to insert control byte in output stream */
  96.  
  97. /*****************************************************************/
  98. /***                   function prototypes                     ***/
  99. /*****************************************************************/
  100.      /* interrupt handler */
  101. void interrupt rs_inthndlr(void);
  102.  
  103.      /* port initialization: Sets up port parameters, installs interrupt
  104.         vector, enables interrupts.  Input and output buffers are 'rotary'
  105.         buffers - buffer size must be a power of 2.  Function returns the
  106.         following:
  107.           4 - Success, 16550 UART, FIFOs enabled.
  108.           3 - Success, 16550 UART, FIFOs unavailable.
  109.           2 - Success, 8250A or 16450 UART
  110.           1 - Success, 8250B UART
  111.           0 - Fail, no UART detected
  112.          -1 - Fail, bad input buffer
  113.          -2 - Fail, bad output buffer
  114.          -3 - Fail, bad port argument
  115.          -4 - Fail, bad parity argument
  116.          -5 - Fail, bad data bits argument
  117.          -6 - Fail, bad stop bits argument
  118.          -7 - Fail, bad baud argument
  119.         Example:
  120.       status = rs_initport(port,baud,parity,bits,stop,in_bufsize,in_bufptr,\
  121.                            out_bufsize,out_bufptr);
  122.       port = '1','2','3', or '4'
  123.       baud = 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600 or 115200
  124.       parity = 'N', 'E', 'O', 'S' or 'M'
  125.       bits = '7' or '8'
  126.       stop = '1' or '2'
  127.       in_bufsize = (power of 2) >= 2 <= 32768
  128.       in_bufptr = char pointer to previously allocated input buffer
  129.       out_bufsize = (power of 2) >= 2 <= 32768
  130.       out_bufptr = char pointer to previously allocated output buffer */
  131. int rs_initport(char, long, char, char, char,
  132.                 unsigned, char *, unsigned, char *);
  133.  
  134.      /* Send single byte out port - if no room in output buffer, wait
  135.         til there's room.  Return 0 on success, or -1 if no room in
  136.         output buffer and output is currently disabled via flow control
  137.         or if port is unavailable.  If RS_POLLED_XMIT is defined, xmit buffer
  138.         and xmit interrupts are not used. */
  139. int rs_sndbyt(int);
  140.  
  141.      /* Send string of specified length out port - if no room in output
  142.         buffer, waits til there's room.  Return 0 on success or number of
  143.         bytes copied to output buffer if not enough room for entire string
  144.         and output is currently disabled via flow control.  Returns -1 if
  145.         port is not available.  If length is 0 (unspecified), send chars
  146.         from string up to but not including terminating nul character.
  147.         If RS_POLLED_XMIT is defined in header, xmit interrupts are not used
  148.         and the function does not return until the string has been sent. */
  149. int rs_sndstr(int,char *);
  150.  
  151.      /* Get single char, return -1 if none available or port is not
  152.         available. */
  153. int rs_getbyt(void);
  154.  
  155.      /* Get specified number of characters from input buffer.  If less
  156.         than the specified number of characters are available, get as
  157.         many as are available.  If length of string is unspecified (0),
  158.         copy characters up to and including terminating nul. Return number
  159.         of characters copied or -1 if port is unavailable.  Null terminate
  160.         string. */
  161. int rs_getstr(int, char *);
  162.  
  163.      /* Return number of characters waiting to be read from input buffer. */
  164. unsigned rs_inrcvd(void);
  165.  
  166.      /* Return amount of free space in output buffer. */
  167. unsigned rs_outfre(void);
  168.  
  169.      /* Return code